package cn.xbtop.blog.controller;

import cn.xbtop.blog.pojo.Log;
import cn.xbtop.blog.service.LogService;
import cn.xbtop.blog.utils.PageBean;
import cn.xbtop.blog.utils.Result;
import cn.xbtop.blog.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/4/14 14:20
 */
@RestController
@RequestMapping("/log")
@Api(description = "日志管理",value = "LogController")
public class LogController {
    @Autowired
    private LogService logService;

    /**
     * 分页查询
     * @param pageBean
     * @return
     */
    @RequestMapping(value = "/findByPage",method = RequestMethod.POST)
    @ApiOperation(value = "分页查询日志",consumes = "分页查询")
    public Result<PageBean<Log>> findByPage(@RequestBody PageBean<Log> pageBean){
        String sortColumn = pageBean.getSortColumn();
        if(StringUtils.isNotBlank(sortColumn)) {
            //排序列不为空
            String sortColumns[] = {"log_url", "log_status", "log_method","created_time"};
            List<String> sortList = Arrays.asList(sortColumns);

            if(!sortList.contains(sortColumn.toLowerCase())){
                return new Result<>(0,"排序参数不合法！");
            }
        }
        pageBean = logService.findByPage(pageBean);
        return new Result<>(pageBean);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public Result<Object> delete(@PathVariable Integer id){
        logService.deleteByPrimaryKey(id);
        return new Result<>("删除成功！");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping(value = "/deleteByIds",method = RequestMethod.PUT)
    public Result<Object> deleteByIds(@RequestBody List<Integer> ids){
        logService.deleteByIds(ids);
        return new Result<>("删除成功！");
    }
}
